{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah', 'Tracy']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取一个list或tuple的部分元素是非常常见的操作\n",
    "L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']\n",
    "[L[0], L[1], L[2]]   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Michael', 'Sarah', 'Tracy']\n"
     ]
    }
   ],
   "source": [
    "# 用循环\n",
    "r = []\n",
    "n = 3\n",
    "for i in range(n):\n",
    "    r.append(L[i]) \n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah', 'Tracy']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片操作符\n",
    "L[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah', 'Tracy']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Sarah', 'Tracy']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Bob', 'Jack']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 倒数第一个元素的索引是-1\n",
    "L[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Bob']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[-2:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 1, 2)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple也可以用切片操作\n",
    "(0, 1, 2, 3, 4, 5)[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABC'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串也可以用切片操作\n",
    "'ABCDEFG'[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练习\n",
    "利用切片操作，实现一个trim()函数，去除字符串首尾的空格，注意不要调用str的strip()方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.递归\n",
    "def trim(s):\n",
    "    if not isinstance(s, str):\n",
    "        raise TypeError('bad operand type')\n",
    "    if s[:1] == ' ':\n",
    "        return trim(s[1:])\n",
    "    if s[-1:-2:-1] == ' ':\n",
    "        return trim(s[:-1])\n",
    "    return s \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "if trim('hello  ') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello  ') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello  world  ') != 'hello  world':\n",
    "    print('测试失败!')\n",
    "elif trim('') != '':\n",
    "    print('测试失败!')\n",
    "elif trim('    ') != '':\n",
    "    print('测试失败!')\n",
    "else:\n",
    "    print('测试成功!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.循环,查找起始位置\n",
    "def trim(s):\n",
    "    b = 0\n",
    "    e = len(s)\n",
    "    for i in range(e):\n",
    "        if s[i] != ' ':\n",
    "            break\n",
    "        b += 1\n",
    "    for i in range(e)[::-1]:\n",
    "        if s[i] != ' ':\n",
    "            break\n",
    "        e = i\n",
    "    return s[b:e]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "if trim('hello  ') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello  ') != 'hello':\n",
    "    print('测试失败!')\n",
    "elif trim('  hello  world  ') != 'hello  world':\n",
    "    print('测试失败!')\n",
    "elif trim('') != '':\n",
    "    print('测试失败!')\n",
    "elif trim('    ') != '':\n",
    "    print('测试失败!')\n",
    "else:\n",
    "    print('测试成功!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
